﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;

namespace MTViewer
{
    /// <summary>
    /// Interaction logic for DbPanel.xaml
    /// </summary>
    public partial class DbPanel : UserControl //, ITouchObject
    {
        //private event EventHandler<PIBV.LoadDrawingEventArgs> OnDropDrawing;

        public DbPanel()
        {
            InitializeComponent();
        }

        public void SetDragDropImage(Image img)
        {
            tagDrawings.InitializeControl(img);
            pidDrawings.InitializeControl(img);
            loopDrawings.InitializeControl(img);
        }

        public void AddDropDrawingEventHandler(EventHandler<PIBV.LoadDrawingEventArgs> handler)
        {
            tagDrawings.OnDropDrawing  += new EventHandler<PIBV.LoadDrawingEventArgs>(handler);
            pidDrawings.OnDropDrawing  += new EventHandler<PIBV.LoadDrawingEventArgs>(handler);
            loopDrawings.OnDropDrawing += new EventHandler<PIBV.LoadDrawingEventArgs>(handler);
            ctrlAction.OnDropDrawing   += new EventHandler<PIBV.LoadDrawingEventArgs>(handler);

        }

        private void btnRotateLeft_Click(object sender, RoutedEventArgs e)
        {
            Transform tr = this.RenderTransform;
            object angle = tr.GetValue(RotateTransform.AngleProperty);
            double new_angle = Convert.ToInt32(angle) - 90;
            tr.SetValue(RotateTransform.AngleProperty, Convert.ToDouble(new_angle));
            SetPosition(Convert.ToInt32(new_angle));
        }

        private void btnRotateRight_Click(object sender, RoutedEventArgs e)
        {
            Transform tr = this.RenderTransform;
            object angle = tr.GetValue(RotateTransform.AngleProperty);
            double new_angle = Convert.ToInt32(angle) + 90;
            tr.SetValue(RotateTransform.AngleProperty, Convert.ToDouble(new_angle));
            SetPosition(Convert.ToInt32(new_angle));
        }

        void SetPosition(int angle)
        {
            int offset = 5; 
            angle = angle % 360;
            Canvas c = TouchObjectFactory.CanvasInstance;
            switch(angle)
            {
                case -360:
                case 0:

                    this.SetValue(Canvas.TopProperty, (double)(c.ActualHeight-this.ActualHeight));
                    this.SetValue(Canvas.LeftProperty, (double)offset);
                    
                    break;
                case -270:
                case 90:

                    this.SetValue(Canvas.TopProperty, (double)offset);
                    this.SetValue(Canvas.LeftProperty, (double)(this.ActualHeight)); 
                  
                    break;
                case -180:
                case 180: 
                    
                    this.SetValue(Canvas.TopProperty, (double)this.ActualHeight);
                    this.SetValue(Canvas.LeftProperty, (double)(c.ActualWidth)); 

                    break;
                case -90:
                case 270: 
                    
                    this.SetValue(Canvas.TopProperty, (double)(c.ActualHeight));
                    this.SetValue(Canvas.LeftProperty, (double)(c.ActualWidth-this.ActualHeight)); 
                    
                    break;
                default: break;
            }
        }

        //public string ImagePath
        //{
        //    get { return (string)GetValue(ImagePathProperty); }
        //    set { SetValue(ImagePathProperty, value); }
        //}

        //public double Angle
        //{
        //    get { return (double)GetValue(AngleProperty); }
        //    set { SetValue(AngleProperty, value); }
        //}

        //public double ScaleX
        //{
        //    get { return (double)GetValue(ScaleXProperty); }
        //    set { SetValue(ScaleXProperty, value); }
        //}

        //public double ScaleY
        //{
        //    get { return (double)GetValue(ScaleYProperty); }
        //    set { SetValue(ScaleYProperty, value); }
        //}

        //public double X
        //{
        //    get { return (double)GetValue(XProperty); }
        //    set { SetValue(XProperty, value); }
        //}

        //public double Y
        //{
        //    get { return (double)GetValue(YProperty); }
        //    set { SetValue(YProperty, value); }
        //}

        //public string PdfFileName
        //{
        //    get { return (string)GetValue(PdfFileNameProperty); }
        //    set { SetValue(PdfFileNameProperty, value); }
        //}

        //public string OriginalImagePath
        //{
        //    get { return (string)GetValue(OriginalImagePathProperty); }
        //    set { SetValue(OriginalImagePathProperty, value); }
        //}

        //#region DependencyProperties

        //// Using a DependencyProperty as the backing store for ImagePath.  This enables animation, styling, binding, etc...
        //public static readonly DependencyProperty ImagePathProperty =
        //    DependencyProperty.Register("ImagePath", typeof(string), typeof(DbPanel), new UIPropertyMetadata(""));

        //// Using a DependencyProperty as the backing store for Angle.  This enables animation, styling, binding, etc...
        //public static readonly DependencyProperty AngleProperty =
        //    DependencyProperty.Register("Angle", typeof(double), typeof(DbPanel), new UIPropertyMetadata(0.0));

        //// Using a DependencyProperty as the backing store for ScaleX.  This enables animation, styling, binding, etc...
        //public static readonly DependencyProperty ScaleXProperty =
        //    DependencyProperty.Register("ScaleX", typeof(double), typeof(DbPanel), new UIPropertyMetadata(1.0));

        //// Using a DependencyProperty as the backing store for ScaleY.  This enables animation, styling, binding, etc...
        //public static readonly DependencyProperty ScaleYProperty =
        //    DependencyProperty.Register("ScaleY", typeof(double), typeof(DbPanel), new UIPropertyMetadata(1.0));

        //// Using a DependencyProperty as the backing store for X.  This enables animation, styling, binding, etc...
        //public static readonly DependencyProperty XProperty =
        //    DependencyProperty.Register("X", typeof(double), typeof(DbPanel), new UIPropertyMetadata(0.0));

        //// Using a DependencyProperty as the backing store for Y.  This enables animation, styling, binding, etc...
        //public static readonly DependencyProperty YProperty =
        //    DependencyProperty.Register("Y", typeof(double), typeof(DbPanel), new UIPropertyMetadata(0.0));

        //// Using a DependencyProperty as the backing store for ImagePath.  This enables animation, styling, binding, etc...
        //public static readonly DependencyProperty PdfFileNameProperty =
        //    DependencyProperty.Register("PdfFileName", typeof(string), typeof(DbPanel), new UIPropertyMetadata(""));

        //// Using a DependencyProperty as the backing store for ImagePath.  This enables animation, styling, binding, etc...
        //public static readonly DependencyProperty OriginalImagePathProperty =
        //    DependencyProperty.Register("OriginalImagePath", typeof(string), typeof(DbPanel), new UIPropertyMetadata(""));
        //#endregion

    }
}
